﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class ScheduleViewModel : INotifyPropertyChanged
    {
        public ScheduleViewModel()
        {
            RefreshEvents();
            this.PropertyChanged += (s, e) => {
                if (insertCurCompetitor != null)
                    insertCurCompetitor.RaiseCanExecuteChanged();
                if (saveCurCompetitor != null)
                    saveCurCompetitor.RaiseCanExecuteChanged();
                if (delCurCompetitor != null)
                    delCurCompetitor.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private IEnumerable<Event> events;
        public IEnumerable<Event> Events
        {
            get { return this.events; }
            set
            {
                this.events = value;
                OnPropertyChange("Events");
            }
        }


        private Event currentEvent;
        public Event CurrentEvent
        {
            get { return this.currentEvent; }
            set
            {
                this.currentEvent = value;
                OnPropertyChange("CurrentEvent");
                RefreshCompetitor();
            }
        }
        private IEnumerable<Competitor> competitors;
        public IEnumerable<Competitor> Competitors
        {
            get { return this.competitors; }
            set
            {
                this.competitors = value;
                OnPropertyChange("Competitors");
            }
        }
        
        private Competitor currentCompetitor = new Competitor();
        public Competitor CurrentCompetitor
        {
            get { return this.currentCompetitor; }
            set
            {
                this.currentCompetitor = value;
                OnPropertyChange("CurrentCompetitor");
            }
        }
       
        #endregion

        #region refresh
        private void RefreshEvents()
        {
            this.ServiceClient.getAllEventCompleted += (s, e) =>
            {
                this.Events = e.Result;
            };
            this.ServiceClient.getAllEventAsync();
        }

        private void RefreshCompetitor()
        {
            //throw new NotImplementedException();
            this.ServiceClient.getAllCompetitorCompleted += (s, e) =>
            {
                this.Competitors = e.Result;
            };
            this.ServiceClient.getAllCompetitorAsync(this.CurrentEvent.EventID);
            

        }
        #endregion
        
        #region Save
        private RelayCommand saveCurCompetitor;
        public ICommand SaveCurCompetitor
        {
            get {return saveCurCompetitor ?? (saveCurCompetitor = new RelayCommand(() => SaveCompetitor(), () => this.currentCompetitor != null && this.currentCompetitor.CompetitorID != 0)); }
        }
        
        private void SaveCompetitor()
        {
            ServiceClient.saveCompetitorAsync(currentCompetitor);
            RefreshCompetitor();
        }

        
        #endregion

        #region del
        private RelayCommand delCurCompetitor;
        public ICommand DelCurCompetitor
        {
            get {return delCurCompetitor ?? (delCurCompetitor = new RelayCommand(() => DelCompetitor(), () => this.currentCompetitor != null && this.currentCompetitor.CompetitorID != 0)); }
        }

        private void DelCompetitor()
        {
            ServiceClient.deleteCompetitorAsync(currentCompetitor);
            RefreshCompetitor();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurCompetitor;
        public ICommand InsertCurCompetitor
        {
            get { return insertCurCompetitor ?? (insertCurCompetitor = new RelayCommand(() => InsertCompetitor(), () => this.currentEvent != null)); }
        }

        private void InsertCompetitor()
        {
            CurrentCompetitor.EventID = currentEvent.EventID;
            ServiceClient.insertCompetitorAsync(CurrentCompetitor);
            RefreshCompetitor();
        }
        #endregion
        
    }
}
